package net.dgg.rz.api.production.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.utils.SearchUtils;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.common.utils.ValidateUtils;
import net.dgg.rz.production.entity.ProductNodeFlow;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.entity.ProductOrderFieldWorkLoad;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.entity.dto.WorkLoadDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.AllOrderService;
import net.dgg.rz.production.service.FlowNodeService;
import net.dgg.rz.production.service.ProductOrderService;
import net.dgg.rz.production.service.ProductOrderWorkLoadService;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import org.noka.mozilla.javascript.IdScriptableObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>@Title APP融资生产接口controller</p>
 * <p>@Version 2.0.0 版本号</p>
 * <p>@author chenyang</p>
 * <p>@date 2019年04月12日</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@RestController
@RequestMapping("/api/rzsc/app")
public class FinancingProductionController extends DggBaseController {

    @Autowired
    private AllOrderService allOrderService;
    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private FlowNodeService flowNodeService;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private CmsService cmsService;
    @Autowired
    private ProductOrderWorkLoadService productOrderWorkLoadService;
    @Autowired
    private UserService userService;
    @Autowired
    private SearchUtils searchUtils;


    /**
     * @return
     * @Description APP 统计融资生产首页信息数量
     * @Author chenyang
     */
    @RequestMapping("/statistic_financing_count")
    public DggRestResponse statisticFinancingDataCount(@RequestParam String userLoginName) {
        try {
            Map<String, Object> resultDataCount = allOrderService.statisticFinancingDataCount(userLoginName);
            return getSuccessResponse(resultDataCount);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Description APP 待接收页面数据接口(重复禁用)
     * @author chenyang
     */

//    @RequestMapping("/wait_received_list")
/*    public DggRestResponse waitReceivedOrderWithPage(@RequestParam Map<String, Object> params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.waitReceivedOrderWithPage(params);
            return this.getSuccessResponse(new HashMap<String, Object>() {{
                put("list", productOrderDtos);
                put("size", params.get("size"));
                put("pageSize", params.get("pageSize"));
            }});
        } catch (CommonExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }*/


    /**
     * @Description APP 退单/废单驳回恢复办理
     * @Author chenyang
     */
    @RequestMapping("/restore_order")
    @ResponseBody
    public Object restoreOrder(String id, String status, String userLoginName) {
        try {
            if (!status.equals(ScProductConstant.RZSCORDER_STATUS06) && !status.equals(ScProductConstant.RZSCORDER_STATUS09) && !status.equals(ScProductConstant.RZSCORDER_STATUS10)) {
                return this.getFailResponse("订单状态传入错误！");
            }
            this.allOrderService.restoreOrder(id, status, userLoginName);
            return this.getSuccessResponse("success!");
        } catch (CommonExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }



    /**
     * @Description APP 待审核订单列表接口
     * @Author chenyang
     */
    @RequestMapping("/wait_audited_list")
    public DggRestResponse waitAuditedList(@RequestParam Map params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.auditOrderAppWithPage(params);
            return this.getSuccessResponse(new HashMap<String, Object>() {{
                put("list", productOrderDtos);
                put("size", params.get("size"));
                put("pageSize", params.get("pageSize"));
            }});
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Description APP  退单审核
     * @author chenyang
     */
    @RequestMapping("/examine_return_orders")
    public DggRestResponse examineReturnOrders(@RequestParam Map params) {
        try {
            productOrderService.abandonAudit(JSON.toJSONString(params));
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * @return
     * @Author Quanlin Wan
     * @Description APP  废单审核
     * @Date 11:22 2018/12/28
     * @Param
     **/
    @RequestMapping("/examine_waste_orders")
    @ResponseBody
    public DggRestResponse examineWasteOrders(@RequestParam Map params) {
        try {

            productOrderService.nullifyAudit(JSON.toJSONString(params));
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * @Description APP 退单，废单审核弹窗之前访问
     * @Author chenyang
     **/
    @RequestMapping("/before_examine_order")
    public DggRestResponse beforeExamineOrder(@RequestParam String id) {
        Map<String, Object> resultData = new HashMap<>();
        try {
            ProductOrder productOrder = productOrderService.selectByPrimaryKey(Long.valueOf(id));
            Map<String, Long> map = new HashMap<>();
            map.put("flowId", productOrder.getNodeFlowId());
            ProductNodeFlow productNodeFlow = flowNodeService.findFlowById(map);
            //翻译字段
            String[] transColum = {"originCode", "status"};
            productOrder = translateUtil.translateObject(transColum, productOrder);
            resultData.put("productNodeFlow", productNodeFlow);
            resultData.put("productOrder", productOrder);
            return getSuccessResponse(resultData);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Description APP 退单、废单审核撤回
     * @Author chenyang
     **/
    @RequestMapping("/revoke_return_waste_orders")
    public DggRestResponse revokeReturnWasteOrders(@RequestParam Map params) {
        try {
            productOrderService.withdrawn(JSON.toJSONString(params));
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Description APP 变更审核人
     * @Author chenyang
     **/
    @RequestMapping("/change_auditor")
    public DggRestResponse changeAuditor(@RequestParam Map params) {
        try {
            productOrderService.changAuditor(JSON.toJSONString(params));
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * APP 查询所有生产订单
     *
     * @Author chenyang
     */
    @RequestMapping(value = "/find_all_orders")
    public DggRestResponse findAllOrders(@RequestParam Map params, HttpServletRequest request) {
        try {
            if (StringUtils.isNotEmpty(params.get("completeState")) && "2".equals(params.get("completeState"))) {//废单
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS08);
                params.put("target", "3");
            } else if (StringUtils.isNotEmpty(params.get("completeState")) && "3".equals(params.get("completeState"))) {//放款
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS08);
                params.put("target", "4");
            } else if (StringUtils.isNotEmpty(params.get("completeState")) && "1".equals(params.get("completeState"))) {//退单
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS06);
                params.put("target", "5");
            } else if (StringUtils.isNotEmpty(params.get("completeState")) && "4".equals(params.get("completeState"))) {
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS09);
                params.put("target", "6");
            } else if (StringUtils.isNotEmpty(params.get("completeState")) && "5".equals(params.get("completeState"))) {
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS10);
                params.put("target", "7");
            } else if (StringUtils.isNotEmpty(params.get("completeState")) && "9".equals(params.get("completeState"))) {
                //statusAttr.add(ScProductConstant.RZSCORDER_STATUS10);
                params.put("target", "9");
            } else {
                params.put("target", "8");                //全部
                List<String> statusAttr = new ArrayList<>();
                if (StringUtils.isNotEmpty(params.get("statusAttr"))) {
                    statusAttr.add(params.get("statusAttr") + "");
                }
                params.put("statusArr", statusAttr);
            }
            Integer isAuthorization = 0;
            UserEntity userEntity = searchUtils.getcurrUser(params);
            ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "未获取到登陆人，请重新登录！");
            if(StringUtils.isNotEmpty(userEntity.getRoleName())){
                if(userEntity.getRoleName().contains("融资外勤人员")){
                    isAuthorization = 1;
                }
            }else{
                ValidateUtils.strNotEmpty(userEntity.getRoleName(), ProductOrderExcption.class, "该用户没有任何角色授权，请联系管理员授权！");
            }
            List<ProductOrderDto> productOrderDtos = productOrderService.findProductOrderWithPage(params);
            Map<String, Object> map =  new HashMap<String, Object>();
            map.put("list", productOrderDtos);
            map.put("size", params.get("size"));
            map.put("pageSize", params.get("pageSize"));
            map.put("isAuthorization", isAuthorization);
            return this.getSuccessResponse(map);
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * APP 生产订单详情页
     *
     * @Author chenyang
     */
    @RequestMapping("/find_order_detail")
    public DggRestResponse findOrderDetail(@RequestParam Long productOrderId, @RequestParam String userLoginName) {
        try {
            Map<String, Object> orderDetail = productOrderService.findOrderDetail(productOrderId, userLoginName);
            return getSuccessResponse(orderDetail);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO处理中列表数据封装
     * @Date 9:14 2019/4/10
     * @Param
     **/
    @PostMapping("/in_process")
    public DggRestResponse inProcessWithPage(@RequestParam Map<String, Object> params) {
        try {
            params.put("target", "10");
            List<ProductOrderDto> productOrderDtos = productOrderService.findProductOrderWithPage(params);
            return this.getSuccessResponse(new HashMap<String, Object>() {{
                put("list", productOrderDtos);
                put("size", params.get("size"));
                put("pageSize", params.get("pageSize"));
            }});
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * @return
     * @Author
     * @Description //APP 待接收页面分页数据
     * @Date 18
     * @Param
     **/

    @RequestMapping("/list.do")
    public DggRestResponse list(@RequestParam Map params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.findProductOrderWithPage(params);
            return this.getSuccessResponse(new HashMap<String, Object>() {{
                put("list", productOrderDtos);
                put("size", params.get("size"));
                put("pageSize", params.get("pageSize"));
            }});
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * APP订单接收回显
     *
     * @param id 生产子订单ID
     * @return
     */
    @RequestMapping("/accept_order.do")
    public DggRestResponse acceptOrder(String id) {
        try {
                //-------------获取当前生产订单数据---------------------------------
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(Long.parseLong(id));
                return this.getSuccessResponse(productOrder);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * APP单个接收订单
     *
     * @param
     * @param
     * @param
     * @return
     */

    @RequestMapping(value = "/single_receive_order.do", method = RequestMethod.POST)
    public DggRestResponse singleReceiveOrder(@RequestParam Map params) {
        try {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(params));
            productOrderService.singleReceiveOrder(jsonObject);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
        return this.getSuccessResponse("接收成功,当前订单已进入办理中");
    }

    /**
     * APP选择商品属性(弃用)
     *
     * @param params
     * @param
     * @return
     */
    @RequestMapping("/product_property.do")
    public DggRestResponse addProduct(@RequestParam String params) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            ValidateUtils.strNotEmpty(jsonObject.getLong("productId"), ProductOrderExcption.class, "产品id不能为空");
            ValidateUtils.strNotEmpty(jsonObject.getString("choosedProduct"), ProductOrderExcption.class, "没有选择对应业务类型或产品");
            Map map = new HashMap();
            map.put("proId", jsonObject.getLong("productId"));
            Map goods = cmsService.productInfo(map);
            return this.getSuccessResponse(goods);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * APP 商品明细查询
     * @param productId
     * @param goodsName
     * @return
     */
    @RequestMapping("/find_goods")
    @ResponseBody
    public DggRestResponse findGoods(Long productId, String goodsName) {
        ValidateUtils.strNotEmpty(productId, ProductOrderExcption.class, "产品ID不能为空");
        List<Map> goodsList =cmsService.ffindGoodsList(new HashMap(){{
            put("productId", productId);
            put("attribute", goodsName);
        }});
        return this.getSuccessResponse(goodsList);
    }


    /**
     * @return
     * @Author
     * @Description //APP 查询废单原因
     * @Date
     * @Param
     **/
    @RequestMapping("/cancellation.do")
    public DggRestResponse findCancellation() {
        try {
            List list = commonMangerExtService.queryTreeBookListByCode(ScProductConstant.FD_YY_CODE, 1, null);
            return this.getSuccessResponse(list);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * @return
     * @Author
     * @Description //APP 申请废单
     * @Date
     * @Param
     **/
    @RequestMapping("/nullify_reason_order.do")
    public Object nullifyReasonOrder(@RequestParam String params) {
        try {
            this.productOrderService.nullifyReasonOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 订单备注(返回客户信息)
     * @return
     */
    /*@NLog
    @RequestMapping(value = "/remark_index")
    public DggRestResponse remarkIndex(Long id) {
        return getSuccessResponse(allOrderServic.getProductById(id));
    }*/

    /**
     * APP订单备注
     *
     * @param id
     * @param remark
     * @return
     */
    //@AuthOpt(code = "SC_GENERAL_RESTORE")
    @RequestMapping("/remark_order.do")
    public Object remarkOrder(Long id, String remark,String userLoginName) {
        try {
            this.allOrderService.remarkOrder(id, remark, userLoginName);
            return this.getSuccessResponse("success!");
        } catch (CommonExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常，请联系管理员！");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常,请联系管理员!");
        }
    }

    /**
     * @return
     * @Author
     * @Description //APP 转单退回
     * @Date 2019/1/10
     * @Param
     **/
    @RequestMapping("/backorder_save")
    public DggRestResponse backorderSave(String id, String remark,String userLoginName) {
        try {
            this.productOrderService.backorderSave(id, remark, userLoginName);
            return this.getSuccessResponse("操作成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 新增产品
     * @Date 13:29 2019/4/15
     * @Param id 生产订单id
     * @Param typeCode业务可类型code
     * @Param productId产品id
     * @Param goodsId商品id
     * @Param channelId渠道id
     * @Param flowUserId流程人员id
     * @Param amount需求金额
     * @Param remark备注
     * @Param channelSubBranch支行（非必填）
     **/

    @RequestMapping("/add_product")
    @ResponseBody
    public DggRestResponse addProduct(@RequestParam Map params) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
            this.productOrderService.addProduct(jsonObject);
            return this.getSuccessResponse("操作成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 更换产品
     * @Date 13:43 2019/4/15
     * @Param
     **/
    @RequestMapping("/change_product")
    @ResponseBody
    public DggRestResponse changeProduct(@RequestParam Map params) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
            this.productOrderService.changeProduct(jsonObject, ScProductConstant.RZSC_ORIGINCODE_PRO_CHANGE);
            return this.getSuccessResponse("操作成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 暂缓恢复办理
     * @param id
     * @return
     */
    @RequestMapping("/restore_order_suspended")
    public Object restoreOrderSuspended(String[] id,String userLoginName) {
        try {
            this.allOrderService.restoreOrderSuspended(id,userLoginName);
            return this.getSuccessResponse("success!");
        }catch (CommonExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常，请联系管理员！");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常,请联系管理员!");
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 更新节点数据回显
     * @Date 16:16 2019/4/17
     * @Param
     **/
    @PostMapping("/openUpdateNode.do")
    public DggRestResponse openUpdateNode(Long id,String userLoginName, @RequestParam(required = false) String nodeId) {

        try {
            Map data = productOrderService.queryDataForUpNode(id, nodeId,userLoginName);
            return this.getSuccessResponse(data);
        } catch (DggBaseException e) {
            return  this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Description 保存节点更新
     * @author: chenyang
     */
    @RequestMapping("/save_node_update")
    public DggRestResponse saveNodeUpdate(@RequestParam Map params) {
        try {
            flowNodeService.saveNodeUpdate(params);
            return this.getSuccessResponse("操作成功");
        } catch (DggBaseException e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
    
    /**
     * @Author Quanlin Wan
     * @Description //TODO 查询生产单是否需要下一审核人
     * @Date 19:15 2019/4/22
     * @Param 
     * @return 
     **/
    @RequestMapping("/need_next_auditor")
    public DggRestResponse needNextAuditor(@RequestParam Long id) {
        try {
            Integer status= productOrderService.needNextAuditor(id);
            return this.getSuccessResponse(status);
        } catch (DggBaseException e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Author sdx
     * @Description //TODO 工作量录入新增页面数据
     * @Date 2019年11月22日11:03:31
     * @Param
     * @return
     **/
    @RequestMapping("/work_entry.do")
    public DggRestResponse workEntry(@RequestParam Long scOrderId,@RequestParam String loginName , @RequestParam Integer recordType) {
        try {
            WorkLoadDto workLoadDto = productOrderWorkLoadService.findModel(scOrderId,loginName,recordType);
            return this.getSuccessResponse(workLoadDto);
        } catch (DggBaseException e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Author sdx
     * @Description //TODO 保存工作量录入数据
     * @Date 2019年11月22日11:03:31
     * @Param
     * @return
     **/
    @RequestMapping("/work_entry_add.do")
    public DggRestResponse workEntryAdd(@RequestParam Long recordType, @RequestParam String loginName, @RequestParam Long scOrderId, @RequestParam Long orgId, @RequestParam String pofwlJson ) {
        try {
            UserEntity userEntity = userService.findUserByLoginName(loginName);
            if(null != userEntity){
                productOrderWorkLoadService.addFieldWorkLoad(recordType, userEntity.getId(), scOrderId, orgId, pofwlJson);
                return this.getSuccessResponse("操作成功");
            }else{
                return this.getFailResponse("没有查询到当前处理人");
            }
        } catch (CommonExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Author sdx
     * @Description //TODO 外勤工作量列表数据
     * @Date 2019年11月22日11:03:31
     * @Param
     * @return
     **/
    @RequestMapping(value = "/field_work_load_data_list.do")
    public DggRestResponse fieldWorkLoadDataList(@RequestParam Map params) {
        try {
            List<ProductOrderFieldWorkLoad> productOrderFieldWorkLoad = productOrderWorkLoadService.findProductOrderFieldWorkLoadWithPage(params);
            return this.getSuccessResponse(new HashMap<String, Object>() {{
                put("list", productOrderFieldWorkLoad);
                put("size", params.get("size"));
                put("pageSize", params.get("pageSize"));
            }});
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Author sdx
     * @Description //TODO 工作量录入编辑页面数据
     * @Date 2019年11月22日11:03:31
     * @Param
     * @return
     **/
    @RequestMapping("/work_entry_update_page.do")
    public DggRestResponse workEntryUpdatePage(@RequestParam Long id, @RequestParam String loginName) {
        try {
            WorkLoadDto workLoadDto = productOrderWorkLoadService.findProductOrderFieldWorkLoad(id,loginName);
            return this.getSuccessResponse(workLoadDto);
        } catch (DggBaseException e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Author sdx
     * @Description //TODO 编辑工作量录入数据保存
     * @Date 2019年11月22日11:03:31
     * @Param
     * @return
     **/
    @RequestMapping("/work_entry_update.do")
    public DggRestResponse workEntryUpdate(@RequestParam String loginName ,@RequestParam String pofwlJson) {
        try {
            productOrderWorkLoadService.updateFieldWorkLoad(loginName,pofwlJson);
            return this.getSuccessResponse("操作成功");
        } catch (CommonExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
}

